package com.express.controller;

import com.express.bean.*;
import com.express.mvc.ResponseBody;
import com.express.service.CourierService;
import com.express.service.ExpressService;
import com.express.service.UserService;
import com.express.util.DateFormatUtil;
import com.express.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: laixiangdong
 * @Date 2020/12/07
 */
public class UserController {
    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, Integer> data = UserService.console();
        Message message = null;
        if (data == null)
            message = new Message(-1, "获取数据失败", null);
        else
            message = new Message(0, "获取数据成功", data);
        String s = JSONUtil.toJSON(message);
        return s;
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp) {
        int offset = 0;
        int pageNumber = 10;
        try {
            // 起始索引值
            offset = Integer.parseInt(req.getParameter("offset"));
            // 查询的数量
            pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        } catch (NumberFormatException e) {
            System.out.println("获取offset，pageNumber值异常，默认值0，10");
        }

        // 查询
        List<User> list = UserService.findAll(true, offset, pageNumber);
        List<BootStrapTableUser> list2 = new ArrayList<>();
        for (User user : list) {
            String createTime = DateFormatUtil.format(user.getCreateTime());
            String loginTime = DateFormatUtil.format(user.getLoginTime());
            BootStrapTableUser user2 = new BootStrapTableUser(user.getId(), user.getUserName(), user.getPassword(), user.getUserPhone(), loginTime, createTime);
            list2.add(user2);
        }
        Map<String, Integer> console = UserService.console();
        Integer total = console.get("userDataSize");

        // 封装为bootstrap-table可识别格式
        ResultData<BootStrapTableUser> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String s = JSONUtil.toJSON(data);
        return s;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) {
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String cardId = req.getParameter("cardId");
        String password = req.getParameter("password");
        User user = new User(userName, password, cardId, userPhone);
        Boolean flag = UserService.insert(user);
        Message message = new Message();
        if (flag) {
            message.setStatus(0);
            message.setResult("添加成功");
        } else {
            message.setStatus(-1);
            message.setResult("添加失败");
        }
        String s = JSONUtil.toJSON(message);
        return s;
    }


    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp) {
        String userPhone = req.getParameter("phone");
        User user = UserService.findByPhone(userPhone);
        Message message = new Message();
        if (user != null) {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(user);
        } else {
            message.setStatus(-1);
            message.setResult("没有查询到数据");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) {
        int id = Integer.parseInt(req.getParameter("id"));
        String cardId = req.getParameter("cardId");
        String password = req.getParameter("password");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        User user = new User();
        user.setUserName(userName);
        user.setUserPhone(userPhone);
        user.setPassword(password);
        user.setCardId(cardId);
        Boolean flag = UserService.update(id, user);
        Message message = new Message();
        if (flag) {
            message.setStatus(0);
            message.setResult("修改成功");
        } else {
            message.setStatus(-1);
            message.setResult("修改失败");
        }
        return JSONUtil.toJSON(message);
    }
    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        Boolean flag = UserService.delete(id);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("删除成功");
        }else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JSONUtil.toJSON(message);
    }
}
